using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;

using VSTalk.Engine.Core.Context;
using VSTalk.Engine.ViewModel.ContactList.ClientResources;
using VSTalk.Engine.ViewModel.ContactList.Filter;
using VSTalk.Engine.ViewModel.ContactList.Order;
using VSTalk.Model;
using VSTalk.Tools;

namespace VSTalk.Engine.ViewModel.ContactList
{
    public class PlotContactLayout : IContactListLayout, IPresentationSource, IListener, INotifyPropertyChanged
    {
        private bool _isActive;
        private IQuerySource _querySource;

        public static IModelContext ModelContext
        {
            get { return AppServices.Get<IModelContext>(); }
        }

        public ClientResourcesManager ResourcesManager { get; set; }

        public PlotContactLayout(IQuerySource querySource)
        {
            _querySource = querySource;
            ActiveResources = new ObservableCollection<IClientResource>();
            ResourcesManager = CreateResourceManager();
        }

        private ClientResourcesManager CreateResourceManager()
        {
            var behaviors = new SearchQueryBehavior(_querySource);
            var manager = new ClientResourcesManager(this, behaviors, behaviors);
            return manager;
        }

        public void StartListen()
        {
            ResourcesManager.StartListen();
        }

        public void StopListen()
        {
            ResourcesManager.StopListen();
        }

        public bool IsActive
        {
            get { return _isActive; }
            private set
            {
                _isActive = value;
                PropertyChanged.Notify(() => IsActive);
            }
        }

        public void Deactivate()
        {
            IsActive = false;
        }

        public void Activate()
        {
            IsActive = true;
        }

        public ObservableCollection<Room> RoomSource
        {
            get
            {
                return ModelContext.Rooms;
            }
        }

        public ObservableCollection<Interlocutor> InterlocutorSource
        {
            get
            {
                return ModelContext.Interlocutors;
            }
        }

        public ObservableCollection<IClientResource> ActiveResources { get; private set; }

        public event PropertyChangedEventHandler PropertyChanged;

    }
}